package com.autoplaza;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;


public class ImageFragment extends Fragment {
    private String imageResourceId;
    public Context ctx;
    ImageView img;
    ProgressBar pbuno;
    int total, position;
    LinearLayout ll_pager_control;
    TextView textview1;
 
	public ImageFragment(String imageResourceId, Context context, int size, int position) {
        this.imageResourceId = imageResourceId;
        ctx = context;
        total = size;
        this.position = position;
    }
    
    public ImageFragment(){}
    
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
    
  
 
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
 
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.image_layout, container, false);
        img = (ImageView) view.findViewById(R.id.imageView1);
        pbuno = (ProgressBar) view.findViewById(R.id.progressBar1);
        ll_pager_control = (LinearLayout) view.findViewById(R.id.ll_pager_control);
        textview1 = (TextView) view.findViewById(R.id.textView1);
        textview1.setText((position+1)+" / "+total);
        for(int i = 0; i < total; i++){
        	Log.i("pager", ""+i);
        	if( i == position){
        		View circle = new View(ctx);
            	circle.setBackgroundResource(R.drawable.circleshape);
            	LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(21,21);
            	params.setMargins(5, 5, 5, 5);
            	circle.setLayoutParams(params);
            	ll_pager_control.addView(circle);
        	}else{
        		View circle = new View(ctx);
            	circle.setBackgroundResource(R.drawable.circleshapegray);
            	LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20,20);
            	params.setMargins(5, 5, 5, 5);
            	circle.setLayoutParams(params);
            	ll_pager_control.addView(circle);
        	}
//        	Button bt = new Button(ctx);
//        	bt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//        	bt.setText("bt"+i);
//        	ll_pager_control.addView(bt);
//        	View circle = new View(ctx);
//        	circle.setBackgroundResource(R.drawable.circleshape);
//        	LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30,30);
//        	params.setMargins(5, 5, 5, 5);
//        	circle.setLayoutParams(params);
//        	ll_pager_control.addView(circle);
        }
        
        Log.i("URL", ""+imageResourceId);
        new CargarFoto().execute(""+imageResourceId);
        
//        BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
//        bmpOptions.inSampleSize = 2;
//        Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/Pediatria/" +  imageResourceId,bmpOptions);
//        img.setImageBitmap(bmp);
        
        
//        util= new Utilities();
//        util.qryImg(ctx, imageResourceId, img);
        
        
        //imageView.setImageResource(imageResourceId);
        return view;
    }
    
    public Bitmap getBitmapFromURL(String src) {
	    try {
	        URL url = new URL(src.replace(" ", "%20"));
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);
	        return myBitmap;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	} 
    
    public class CargarFoto extends AsyncTask<String, Void, Bitmap>{

		@Override
		protected Bitmap doInBackground(String... params) {
			Bitmap image = getBitmapFromURL(params[0]);
			return image;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			pbuno.setVisibility(View.GONE);
			img.setImageBitmap(result);
			
			super.onPostExecute(result);
		}
    	
    	
    }
}
